/*
 * DigaltalLogicRelay.h
 *
 *  Created on: 2012. 10. 16.
 *      Author: dalxx
 */
#include "PLCConfig.h"

#ifndef DIGALTALLOGICRELAY_H_
#define DIGALTALLOGICRELAY_H_

#include "Relay.h"
#include "../StringStream.h"


namespace PLCLib {
class RelayContainer;

class RelayDL;
typedef bool (*operationHandler)(bool*, bool*);

struct BooleanOperation
{
	operationHandler operation;
	bool* param1;
	bool* param2;
	bool val;
};

struct DL{
	bool* val;
	bool nextStatus;
};

class RelayDL {
protected:

	Relay* relayList;
	DL* DLList;
	int numRelay;
	RelayContainer* container;
	static BooleanOperation operationBuffer[MAX_OPERATIONSIZE];
	static int nextBuffer;
public:
	RelayDL();
	~RelayDL();
	void init(int num_relay, Relay* relay_list, RelayContainer* relay_container);
	void removeAll();
	void calculateNext();
	void setRelay(int index, char* logic, int size);
	void commit();
public:
	static void calcuateOperation();
protected:

	bool* buildOperation(StringStream& stream);
	bool* pasrseOneTerm(StringStream& stream);
	int eatParenthesis(Stream& stream);
	static BooleanOperation* getNextOperationBuffer();


private:


	static bool _and(bool* p1, bool* p2);
	static bool _or(bool* p1, bool* p2);
	static bool _not(bool* p1, bool* p2);
	static bool _ref(bool* p1, bool* p2);
};

} /* namespace PLCLib */
#endif /* DIGALTALLOGICRELAY_H_ */
